package com.zjun.service.impl;

import com.zjun.mapper.MainDataMapper;
import com.zjun.pojo.MainDataPO;
import com.zjun.service.FrontendDataService;
import com.zjun.utils.FormatterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FrontendDataServiceImpl implements FrontendDataService {

    //    调用mapper接口
    @Autowired
    private MainDataMapper mainDataMapper;




    //计算每个类别的支出金额
    @Override
    public HashMap<String,String> GetTotalPrice() {


        // 获取所有数据
        List<MainDataPO> mainDataPOList = mainDataMapper.select();

        //对支出类型进行去重
        List<String> costTypeList = new ArrayList<>();
        for (MainDataPO entity : mainDataPOList) {
            String costType = entity.getCostType(); //添加到数组中
            costTypeList.add(costType);
        }
        Set<String> uniqueType = new HashSet<>(costTypeList);

        //计算每种类型的金额
        HashMap<String, String> typeMap = new HashMap<>();
        for (String type : uniqueType) {
            double CostPriceSum = 0; //累加支出金额
            for (MainDataPO entity : mainDataPOList) {
                // 计算每一类支出类型的支出金额
                if (entity.getCostType().equals(type)) {
                    CostPriceSum = CostPriceSum + entity.getCostPrice();
                }

            }
            String PriceSum = FormatterUtils.NumFormat(CostPriceSum);
            typeMap.put(type, PriceSum); //添加到map中
        }



        return typeMap;
    }
}
